/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/18
 */

import Vue from 'vue';

import Validator from '../../../../grsc-base/component/Validator/main';
import Loading from '../../../../grsc-base/component/Loading.vue';
import {PAGE_SIZE, REG_NUMBER,REG_EN} from '../../../../grsc-base/common/constants';

import AssetDrop from '../asset/AssetDrop.vue'

import tpl from './tpl.html';
import service from './service';

export default Vue.extend({
    name: "Wallet",
    template: tpl,
    components: {
        Loading,
        Validator,
        AssetDrop
    },

    data() {
        return {
            isLoading: true,
            isAddressLoading: true,
            coinType: null,
            coinList: null,
            coinAddressList: null,
            isRequesting: false,
            form: {
                tips: '',
                address: ''
            },
            rules: {
                tips: {
                    required: true,
                    requiredTip: this.$t('wallet.tipsRequireTip')
                },
                address: {
                    tip: '<span class="error">' + this.$t('asset.withdraw.tipAddress') + '</span>',
                    required: true,
                    requiredTip: this.$t('common.coinAddressRequireTip', {coin: this.coinType}),
                    pattern: REG_EN,
                    patternTip: window.CODE.INVALID_COIN_ADDREESS[this.$i18n.locale]
                }
            }
        }
    },
    created() {
        this._fetchCoinList();
    },
    mounted(){
    },
    methods: {
        /**
         * 获取开放充值的列表
         * */
        _fetchCoinList() {
            let that = this;
            service.withdrawAssetList().done(rep=>{
                if(rep.code === window.CODE.SUCCESS){

                    let results = [];
                    let withdrawList = rep.data;
                    $.map(this.$$assetList, (item, index) => {
                        $.map(withdrawList, (innerAssetCode, innerIndex) => {
                            if (item.assetCode === innerAssetCode) {
                                results.push(item.assetCode);
                            }
                        });
                    });

                    this.coinList = results;
                    _doCoinType();
                    this._fetchCoinAddressList();
                }

                function _doCoinType() {
                    that.coinList.sort();
                    console.log(that.$route.params.coinType)
                    if(that.$route.params.coinType == 'init') {
                        that.coinType=that.coinList[0]
                    } else {
                        that.coinType=that.$route.params.coinType.toUpperCase();
                    }
                   
                    console.log(that.coinType)
                }
            })
        },
        gosel(coin,add) {
            localStorage.setItem('cointype2',coin);
            window.location.href = `#/withdraw/${coin}?address=${add}`
        },
        /**
         * 钱包地址列表
         * @private
         */
        _fetchCoinAddressList() {
            this.isAddressLoading = true;
            let param = {
                assetCode: this.coinType,
                pageNo: 1,
                pageSize: 5
            };
            service.transferOutAddressQuery(param).done(rep=>{
                if(rep.code === window.CODE.SUCCESS){
                    this.coinAddressList = rep.data.list;
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {
                window.error(rep.msg);
            }).always(()=>{
                this.isAddressLoading = false;
            })
        },
        changeCoinType(coinType) {
            this.coinType = coinType;
            this._fetchCoinAddressList();
            // this.$router.push({ name: 'wallet', params: { coinType: this.coinType }});
            // this.reload();
        },
        deleteAddress(id, $event) {
            window.confirm({
                target: $event.target,
                content: this.$t('wallet.areYouSure'),
                confirmBtnText:this.$t('common.yes'),
                cancelBtnText:this.$t('common.no'),
            }).done(() => {
                service.transferOutAddressDelete({
                    id: id
                }).done(rep=>{
                    if(rep.code === window.CODE.SUCCESS){
                        window.success(rep.msg);
                        this._fetchCoinAddressList();
                    }else{
                        window.error(rep.msg);
                    }
                }).fail((rep) => {
                    window.error(rep.msg);
                }).always(() => {

                });
            });
        },

        /**
         * 重置表单
         * @private
         */
        _resetForm() {
            this.form.address = '';
            this.form.tips = '';
        },

        /**
         * 添加新地址
         * @returns {boolean}
         */
        submit() {
            if (this.rules.$checkAll() === false) {
                return true;
            }
            this.isRequesting = true;

            let param = {
                assetCode: this.coinType,
                address: this.form.address,
                memo: this.form.tips
            };
            service.transferOutAddressAdd(param).done(rep=>{
                if(rep.code === window.CODE.SUCCESS){
                    window.success(rep.msg);
                    this._resetForm();
                    this._fetchCoinAddressList();
                }else{
                    window.error(rep.msg);
                }
            }).fail((rep) => {
                window.error(rep.msg);
            }).always(() => {
                this.isRequesting = false;
            });
        }

    },
    watch: {}
});






